<?php

require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 1;
$page_title = 'JANSEN';
$guide_message = 'Product | Category';
$out_ary = array();

getMyParam('action');
getMyParam('hdLevel');
getMyParam('Parent'.$hdLevel);

//echo $_FILES['up_pic']['type'];exit;
if ($action != "") {
    if ($action == 'add') {
        if (!empty($_POST['name'])) {            

            $seq = is_numeric($_POST['seq']) && $_POST['seq'] > 0 ? $_POST['seq'] : 0;
			$ParentName = "Parent".$hdLevel;
			$fid = $$ParentName;
			
			//$sql = "INSERT INTO `category` (`code`, `fid`, `name`, `pic`, `pic_2`, `e_catalogue`, `seq`, `description`, `publish`) VALUES ('" . $_POST['code'] . "', '$fid', '" . $_POST['name'] . "', '$file_name', '$file_name2', '$e_file_name', '$seq', '" . $_POST['description'] . "', '" . $_POST['publish'] . "')";
			//$sql = "INSERT INTO `category` (`code`, `fid`, `name`,`seq`,`publish`) VALUES ('" . $_POST['code'] . "', '$fid', '" . $_POST['name'] . "', '$seq', '". $_POST['publish'] . "')";
			$sql = "INSERT INTO `category` ( `fid`, `name`,`namezh`,`seq`,`publish`) VALUES ( '$fid', '" . $_POST['name'] ."','". $_POST['name']. "', '$seq', '". $_POST['publish'] . "')";
            $db->query($sql);
			
			//д뻺ļ
			category_cache();
        }
    }

    if ($action == 'edit') {
        getMyParam('cid');
        if (is_numeric($cid) && $cid > 0 && !empty($_POST['name'])) {
            $seq = is_numeric($_POST['seq']) && $_POST['seq'] > 0 ? $_POST['seq'] : 0;
            $change_col = '';
			
			$level = $_POST["hdLevel"];
			$fid = $_POST["Parent".$level];
            
            $sql = "SELECT * FROM `category` WHERE `id` = '" . $cid . "'";
            $old_category_ary = $db->get_one($sql);                
            

			$sql = "UPDATE `category` SET
					`fid` = '" . $fid . "', `name` = '" . $_POST['name'] . "', `namezh` = '" . $_POST['namezh'] . "', `seq` = '$seq',`publish` = '" . $_POST['publish'] . "' " . $change_col . " 
                        WHERE `id` = '$cid'";
            //echo $sql;exit;
            $db->query($sql);
			
			//д뻺ļ
			category_cache();
        }
        
        goFromPage();
    }

    if ($action == 'del') {
        getMyParam('cid');
        if (is_numeric($cid) && $cid > 0) {
            $sql = "DELETE FROM `category` WHERE `id` = '" . $cid . "'";
			$db->query($sql);
			
			//д뻺ļ
			category_cache();
        }
        
        goFromPage();
    }

    header("LOCATION:p_category.php");
}

// *** page code start ***
//$page_url = 'p_category.php?';
//
//$sql = "SELECT `id` FROM `category`";
//$query = $db->query($sql);
//$sql_all_num = $db->num_rows($query);
//
//$ppp = 100;
//getMyParam('page');
//$page = empty($page) || !ispage($page) ? 1 : $page;
//$start_limit = ($page - 1) * $ppp;
//if($start_limit > $sql_all_num) {
//    $start_limit = 0;
//    $page = 1;
//}
//
//$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

// *** page code end ***

//$sql = "SELECT `id`, `code`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = 0 ORDER BY `seq` ASC LIMIT $start_limit, $ppp";
//$query = $db->query($sql);
//while($rs = $db->fetch_array($query)) {
//    if ($rs['code'] == 0) {
//        $rs['code'] = '';
//    }
//    $out_ary[] = $rs;
//}
//
//$sql = "SELECT `id`, `code`, `fid`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` > 0 ORDER BY `seq` ASC";
//$query = $db->query($sql);
//while($rs = $db->fetch_array($query)) {
//    $sub_category_ary[$rs['fid']][] = $rs;
//}
//

//д뻺,ӻȡ
function GetCategoryTr($aryTmp,$level)
{
	$strTmp = '';
	$strTab = '&nbsp;|--&nbsp;';
	for($i=1;$i<$level;$i++)
	{
		$strTab = "&nbsp;&nbsp;&nbsp;&nbsp;".$strTab;
	}
	
	if(is_array($aryTmp))
	{
		foreach($aryTmp as $k => $v)
		{
		   //<td>' . $strTab . $v['namezh'] . '</td>
			$strTmp .= '<tr class="tbcontent1"> 
					<td align="center">' . $v['id'] . '</td>
					<td>' . $strTab . $v['name'] . '</td>					
					<td align="center">' . $v['seq'] . '</td>
					<td align=center bgcolor="ECECEC">
					<a href="javascript:getAdminEdit(\'edit_area\', \'1\', \'' . $v['id'] . '\')"><img src="../images/admin/bt_edit.gif" border="0" alt="Edit"></a>
					<a href="p_category.php?action=del&cid=' . $v['id'] . '" onclick="return confirm(\'Delete ' . $v['name'] . '?\')"><img src="../images/admin/bt_delete.gif" border="0" Alt="Delete"></a>
					</td>
					</tr>';
			if(is_array($v["sub_cate"]) && count($v["sub_cate"]) > 0)
			{
				$strTmp .= GetCategoryTr($v["sub_cate"],$level+1);
			}		
		}
	}
	return $strTmp;
}
$out_str = "";
$aryCategories = get_category();
foreach($aryCategories as $key => $value)
{
   //<td bgcolor="ECECEC">'.$value["namezh"].'&nbsp;</td>
	$out_str .= '<tr class="tbcontent1"> 
                <td align="center" bgcolor="ECECEC">'.$value["id"].'</td>
			<td bgcolor="ECECEC">'.$value["name"].'</td>
			
                <td align="center" bgcolor="ECECEC">'.$value["seq"].'</td>
			    <td align=center bgcolor="ECECEC">
			<a href="javascript:getAdminEdit(\'edit_area\', \'1\', \''.$value["id"].'\')"><img src="../images/admin/bt_edit.gif" border="0" alt="Edit"></a>
			<a href="p_category.php?action=del&cid='.$value["id"].'" onclick="return confirm(\'Delete '.$value["name"].'?\')"><img src="../images/admin/bt_delete.gif" border="0" Alt="Delete"></a>
			<!--<a href="enquire.php?id='.$value["id"].'"><img src="../images/admin/bt_enquiry.gif" border="0" Alt="Enquire"></a>--></td>
              </tr>';	
	if(is_array($value["sub_cate"]) && count($value["sub_cate"]) > 0)
	{
		$out_str .= GetCategoryTr($value["sub_cate"],1);
	}
}


$debug = 1;
debuginfo();


include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_p_category');
require_once PrintEot('a_footer');

?>